package servlet;

import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        resp.getWriter().print(username);
        resp.getWriter().print(password);
        String identity = null;
        UserService service = new UserServiceImpl();

        try {
            identity = service.select(username,password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        session.setAttribute("username",username);
        session.setAttribute("identity",identity);

        if(identity.equals("student")){
            resp.sendRedirect("/lessonListServlet");
        }else{
            resp.sendRedirect("/ratifyLessonListServlet");
        }
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
